function names = fieldnames(S)
% BVQXfile::fieldnames  - overloaded method (completion, etc.)
%
% method used for getting property list (and methods), especially
% for the auto completion feature in the GUI

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% persistent methods
global bvqxlast;
persistent bvqxfile_methods;
if isempty(bvqxfile_methods)
    bvqxfile_methods = BVQXfile(0, 'methods');
end

% only valid if not empty
if isempty(S)
    names = {};
    return;
end

% get structed version and lookup
sfile = struct(S);
if ~isempty(sfile)
    try
        last = sfile(1).L;
        sc = bvqxfile_getscont(last);
    catch
        error( ...
            'BVQXfile:InvalidObject', ...
            'Memory of object freed or invalid object.' ...
        );
    end
end
pril = (last == bvqxlast(2));
bvqxlast = [last, bvqxlast(1)];

% get file type
stype = lower(sc.S.Extensions{1});

% return original names
names = fieldnames(sc.C);

% add methods if any
if isfield(bvqxfile_methods, stype)
    tm = bvqxfile_methods.(stype);
    mf = fieldnames(tm);

    % iterate
    nnames = numel(names);
    names(nnames + numel(mf)) = names(nnames);
    for mc = 1:numel(mf)
        sm = tm.(mf{mc}){1};
        up = find(sm == '_');
        if pril
            names{nnames + mc} = [sm(up(1)+1:end) tm.(mf{mc}){2}];
        else
            names{nnames + mc} = sm(up(1)+1:end);
        end
    end
end

% add AFT methods
tm = bvqxfile_methods.aft;
mf = fieldnames(tm);

% iterate
nnames = numel(names);
names{nnames + numel(mf)} = '';
for mc = 1:numel(mf)
    sm = tm.(mf{mc}){1};
    if pril
        names{nnames + mc} = [sm(5:end) tm.(mf{mc}){2}];
    else
        names{nnames + mc} = sm(5:end);
    end
end
